<?php

namespace App\Http\Controllers\Bap;
use App\Models\Autonym;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Cloud;
use Exception;
use DB;


/**
 * 实名认证
 */
class AutonymController extends Controller
{
    /**
     * 实名认证显示页面
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/14
     * @access public
     * @param  int    uid    用户ID
     * @return array        用户实名认证信息
     */
    public function autonym_status(Request $request)
    {
        $uid = 3;
        $info = $request->all();
        if (!empty($info['uid'])){
            $uid = $info['uid'];
        }else{
            return '参数错误！';
        }
        $autonym_user = Autonym::where('uid',$uid)->first();

        if (empty($autonym_user)){
            return view('api/autonym/autonym_index', compact('uid'));
        }

        $type = $autonym_user->status;

        switch ($autonym_user->status) {
            case 1: //待审核
                return view('api/autonym/autonym_status',compact('type'));
                break;
            case 2: //审核通过
                return view('api/autonym/autonym_status', compact('autonym_user','type'));
                break;
            case 3: //审核未通过
                return view('api/autonym/autonym_status', compact('uid','type'));
                break;
        }
    }

    public function autonym_index(Request $request)
    {
        $uid = 1;
        $info = $request->all();

        if (!empty($info['uid'])) {
            $uid = $info['uid'];
        }else{
            return '参数错误！';
        }

        return view('api/autonym/autonym_index', compact('uid'));
    }

    /**
     * 实名认证提交
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/15
     * @access public
     * @param  mixed    num    身份证号码
     * @param  int    uid    用户ID
     * @param  string    img1    身份证正面图片路径
     * @param  string    img2    身份证反面图片路径
     * @return array        提示信息
     */
    public function autonym_submit(Request $request)
    {
        $info = $request->all();
        if (!$request->has('uid','name','num','img1','img2')) {
            return respF(1028);
        }
        $uid = $info['uid'];
        //省份证验证
        if (!isIdentity($info['num'])) {
            return respF(2027);
        }
        $data['name'] = $info['name'];
        $data['indentity_card'] = $info['num'];
        $data['back_card'] = $info['img2'];
        $data['front_card'] = $info['img1'];
        $data['time'] = time();
        $autonym_user = Autonym::where('uid',$uid)->first();
        //实名认证未填写
        if (!$autonym_user) {
            $data['uid'] = $info['uid'];
            $re = Autonym::create($data);
        } else if ($autonym_user->status==3) { //实名认证未通过
            $re = Autonym::where('uid',$uid)->update($data);
        }else{
            return respF(2028);
        }

        if ($re){
            return ['msg'=>'上传成功','status'=>1];
        }
         
        return respF(2028);
    }
}
